<template>
  <el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
    <el-form :model="form" :rules="rules" :readonly="mode=='show'"  ref="dialogForm" label-width="100px">
		<!-- :disabled="mode=='show'" -->
      <el-form-item label="ID" prop="id">
        <el-input v-model="form.id" placeholder="请输入ID" clearable></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input v-model="form.createTime" placeholder="请输入创建时间" clearable></el-input>
      </el-form-item>
      <el-form-item label="账号" prop="username">
        <el-input v-model="form.username" placeholder="请输入账号" clearable></el-input>
      </el-form-item>
      <el-form-item label="账号ID" prop="userId">
        <el-input v-model="form.userId" placeholder="请输入账号ID" clearable></el-input>
      </el-form-item>
      <el-form-item label="操作IP" prop="ipaddr">
        <el-input v-model="form.ipaddr" placeholder="请输入操作IP" clearable></el-input>
      </el-form-item>
      <el-form-item label="浏览器" prop="browser">
        <el-input v-model="form.browser" placeholder="请输入浏览器" clearable></el-input>
      </el-form-item>
      <el-form-item label="操作系统" prop="os">
        <el-input v-model="form.os" placeholder="请输入操作系统" clearable></el-input>
      </el-form-item>
      <el-form-item label="操作状态" prop="status">
		<span v-if="form.status==1" plain size="small" text>
			<sc-status-indicator type="success"></sc-status-indicator> 成功
		</span>
		<span v-if="form.status!=1" plain size="small" text>
			<sc-status-indicator type="danger"></sc-status-indicator> 失败
		</span>
      </el-form-item>
      <el-form-item label="操作功能" prop="operateName">
        <el-input v-model="form.operateName" placeholder="请输入操作功能" clearable></el-input>
      </el-form-item>
      <el-form-item label="请求方法" prop="requestMethod">
        <el-input v-model="form.requestMethod" placeholder="请输入请求方法" clearable></el-input>
      </el-form-item>
      <el-form-item label="请求路径" prop="requestPath">
        <el-input v-model="form.requestPath" placeholder="请输入请求路径" clearable></el-input>
      </el-form-item>
      <el-form-item label="异常描述" prop="errorMsg">
        <el-input type="textarea" :rows="5" v-model="form.errorMsg" placeholder="请输入异常描述" clearable></el-input>
      </el-form-item>
      <el-form-item label="请求参数" prop="requestParam">
        <el-input type="textarea" :rows="5" v-model="form.requestParam" placeholder="请输入请求参数" clearable></el-input>
      </el-form-item>
      <el-form-item label="请求数据" prop="requestData">
        <el-input type="textarea" :rows="5" v-model="form.requestData" placeholder="请输入请求数据" clearable></el-input>
      </el-form-item>
      <el-form-item label="响应数据" prop="responseData">
        <el-input type="textarea" :rows="5" v-model="form.responseData" placeholder="请输入响应数据" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible=false" >取 消</el-button>
      <el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
    import setting from './setting'
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id: null,
					createTime: null,
					username: '',
					userId: null,
					ipaddr: '',
					browser: '',
					os: '',
					status: 0,
					operateName: '',
					requestMethod: '',
					requestPath: '',
					errorMsg: '',
					requestParam: '',
					requestData: '',
					responseData: '',
					_: ""
				},
				//验证规则
				/*
				rules: {
					sort: [
						{required: true, message: '请输入排序', trigger: 'change'}
					],
					name: [
						{required: true, message: '请输入名称'}
					]
				},
				*/
			}
		},
		mounted() {
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res;
						if (this.mode == "edit"){
							res = await setting.api.edit.post(this.form)
						} else {
							res = await setting.api.add.post(this.form)
						}
						this.isSaveing = false;
						if(res.code == setting.successCode){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.createTime = data.createTime
				this.form.username = data.username
				this.form.userId = data.userId
				this.form.ipaddr = data.ipaddr
				this.form.browser = data.browser
				this.form.os = data.os
				this.form.status = data.status
				this.form.operateName = data.operateName
				this.form.requestMethod = data.requestMethod
				this.form.requestPath = data.requestPath
				this.form.errorMsg = data.errorMsg
				this.form.requestParam = data.requestParam
				this.form.requestData = data.requestData
				this.form.responseData = data.responseData

				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.form, data)
			}
		}
	}
</script>

<style>
</style>
